package com.turnengine.client.global.game.bean;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Game Definition.
 */
public class GameDefinition implements IGameDefinition, Comparable<IGameDefinition> {

	/** The game. */
	private IGame game;
	/** The version. */
	private IGameVersion version;
	/** The instance. */
	private IGameInstance instance;
	/** The host. */
	private IGameHost host;

	/**
	 * Creates a new Game Definition.
	 */
	public GameDefinition() {
	}

	/**
	 * Creates a new Game Definition.
	 * @param game the game
	 * @param version the version
	 * @param instance the instance
	 * @param host the host
	 */
	public GameDefinition(IGame game, IGameVersion version, IGameInstance instance, IGameHost host) {
		setGame(game);
		setVersion(version);
		setInstance(instance);
		setHost(host);
	}

	/**
	 * Creates a new Game Definition.
	 * @param gameDefinition the game definition
	 */
	public GameDefinition(GameDefinition gameDefinition) {
		setGame(gameDefinition.getGame());
		setVersion(gameDefinition.getVersion());
		setInstance(gameDefinition.getInstance());
		setHost(gameDefinition.getHost());
	}

	/**
	 * Creates a new Game Definition.
	 * @param iGameDefinition the i game definition
	 */
	public GameDefinition(IGameDefinition iGameDefinition) {
		setGame(iGameDefinition.getGame());
		setVersion(iGameDefinition.getVersion());
		setInstance(iGameDefinition.getInstance());
		setHost(iGameDefinition.getHost());
	}

	/**
	 * Returns the game.
	 * @return the game.
	 */
	public IGame getGame() {
		return game;
	}

	/**
	 * Returns the version.
	 * @return the version.
	 */
	public IGameVersion getVersion() {
		return version;
	}

	/**
	 * Returns the instance.
	 * @return the instance.
	 */
	public IGameInstance getInstance() {
		return instance;
	}

	/**
	 * Returns the host.
	 * @return the host.
	 */
	public IGameHost getHost() {
		return host;
	}

	/**
	 * Sets the game.
	 * @param game the game to set.
	 */
	public void setGame(IGame game) {
		this.game = game;
	}

	/**
	 * Sets the version.
	 * @param version the version to set.
	 */
	public void setVersion(IGameVersion version) {
		this.version = version;
	}

	/**
	 * Sets the instance.
	 * @param instance the instance to set.
	 */
	public void setInstance(IGameInstance instance) {
		this.instance = instance;
	}

	/**
	 * Sets the host.
	 * @param host the host to set.
	 */
	public void setHost(IGameHost host) {
		this.host = host;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(61, 37);
		hash.append(game);
		hash.append(version);
		hash.append(instance);
		hash.append(host);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof GameDefinition) {
			GameDefinition compare = (GameDefinition) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.game, compare.game);
			equals.append(this.version, compare.version);
			equals.append(this.instance, compare.instance);
			equals.append(this.host, compare.host);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IGameDefinition compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.game, compare.getGame());
		builder.append(this.version, compare.getVersion());
		builder.append(this.instance, compare.getInstance());
		builder.append(this.host, compare.getHost());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		if (game != null) {
			string.append("game", game);
		}
		if (version != null) {
			string.append("version", version);
		}
		if (instance != null) {
			string.append("instance", instance);
		}
		if (host != null) {
			string.append("host", host);
		}
		return string.toString();
	}
}
